package org.huzhp.oauth.client.shiro;

import org.apache.shiro.session.SessionException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description: oauthlogin filter
 * @Author: huzp
 * @CreateDate: 2020/4/7 14:48
 * @Version: 1.0
 */
public class OauthLogoutFilter extends LogoutFilter {

    private String matchUrl = "/oauth/logout";

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest req = (HttpServletRequest) request;
        if (!isMatchUrl(req.getRequestURI())) {
            return true;
        }

        Subject subject = this.getSubject(request, response);
        String redirectUrl = this.getRedirectUrl(request, response, subject);
        try {
            subject.logout();
        } catch (SessionException var6) {
        }
        this.issueRedirect(request, response, redirectUrl);
        return false;
    }

    private boolean isMatchUrl(String url) {
        return matchUrl.equals(url);
    }

    public void setMatchUrl(String matchUrl) {
        this.matchUrl = matchUrl;
    }
}
